﻿using System;

namespace NamedBinaryTag.Converters
{
	class FixedTokenSource : AbstractTokenSource
	{
		private Token[] _tokens;
		private int _offset;

		internal FixedTokenSource(Token[] tokens)
		{
			_tokens = tokens;
		}

		internal void Reset()
		{
			_offset = 0;
		}

		internal override Token LastToken
		{
			get { if ((_offset > 0) && (_offset <= _tokens.Length)) return _tokens[_offset - 1]; return null; }
		}

		internal override Token ReadAhead(int offset)
		{
			if (offset < 0)
				throw new ArgumentOutOfRangeException("offset", "Must be a positive or zero integer.");
			if (_offset + offset < _tokens.Length)
				return _tokens[_offset + offset];
			return null;
		}

		internal override Token ReadNext()
		{
			if (_offset <= _tokens.Length)
				_offset++;
			return LastToken;
		}
	}
}
